import { getCurrentInstance } from "vue";

/**
 * @name 校验是否为空
 * @param {*} val 校验值
 * @returns {boolean}
 */
export function validateNull(val) {
  if (typeof val == "boolean") {
    return false;
  }
  if (typeof val == "number") {
    return false;
  }
  if (val instanceof Array) {
    if (val.length == 0) return true;
  } else if (val instanceof Object) {
    if (JSON.stringify(val) === "{}") return true;
  } else {
    if (val == "null" || val == null || val == "undefined" || val == undefined || val == "")
      return true;
    return false;
  }
  return false;
}
/**
 * validate email
 * @param email
 * @returns {boolean}
 */
export function validateEmail(email) {
  const re = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
  return re.test(email);
}

/**
 * 格式化时间
 * @param {string|Date} date - 时间字符串或Date对象
 * @param {string} format - 格式化模式，默认为 'YYYY-MM-DD HH:mm:ss'
 * @returns {string} 格式化后的时间字符串
 */
export function formatTime(date, format = 'YYYY-MM-DD HH:mm:ss') {
  if (!date) return '';
  
  const d = new Date(date);
  if (isNaN(d.getTime())) return '';
  
  const year = d.getFullYear();
  const month = String(d.getMonth() + 1).padStart(2, '0');
  const day = String(d.getDate()).padStart(2, '0');
  const hours = String(d.getHours()).padStart(2, '0');
  const minutes = String(d.getMinutes()).padStart(2, '0');
  const seconds = String(d.getSeconds()).padStart(2, '0');
  
  // 根据不同格式返回相应的时间字符串
  switch (format) {
    case 'YYYY-MM-DD':
      return `${year}-${month}-${day}`;
    case 'MM-DD':
      return `${month}-${day}`;
    case 'HH:mm':
      return `${hours}:${minutes}`;
    case 'HH:mm:ss':
      return `${hours}:${minutes}:${seconds}`;
    case 'YYYY-MM-DD HH:mm':
      return `${year}-${month}-${day} ${hours}:${minutes}`;
    default:
      return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
  }
}

/**
 * 格式化相对时间（如：刚刚、5分钟前、1小时前等）
 * @param {string|Date} date - 时间字符串或Date对象
 * @returns {string} 相对时间字符串
 */
export function formatRelativeTime(date) {
  if (!date) return '';
  
  const d = new Date(date);
  if (isNaN(d.getTime())) return '';
  
  const now = new Date();
  const diff = now.getTime() - d.getTime();
  
  const minute = 60 * 1000;
  const hour = 60 * minute;
  const day = 24 * hour;
  const week = 7 * day;
  const month = 30 * day;
  const year = 365 * day;
  
  if (diff < minute) {
    return '刚刚';
  } else if (diff < hour) {
    return `${Math.floor(diff / minute)}分钟前`;
  } else if (diff < day) {
    return `${Math.floor(diff / hour)}小时前`;
  } else if (diff < week) {
    return `${Math.floor(diff / day)}天前`;
  } else if (diff < month) {
    return `${Math.floor(diff / week)}周前`;
  } else if (diff < year) {
    return `${Math.floor(diff / month)}个月前`;
  } else {
    return `${Math.floor(diff / year)}年前`;
  }
}

export function usePureListenersAndAttr() {
  const vm = getCurrentInstance();
  if (!vm) {
    throw new Error("useListeners must be called in setup");
  }
  if (!vm.attrs) {
    throw new Error("vm not have attrs");
  }
  const pureListeners = {},
    pureAttrs = {};
  const attrs = vm.attrs || vm.proxy.$attrs;
  Object.keys(attrs).forEach((key) => {
    if (/^on[A-Z]/.test(key) && typeof attrs[key] === "function") {
      // 将onXxAa 转换为 xxAa
      const keyName = key.replace(/^on[A-Z]/, (s) => s.charAt(2).toLowerCase());
      pureListeners[keyName] = attrs[key];
    } else {
      pureAttrs[key] = attrs[key];
    }
  });
  return {
    pureAttrs,
    pureListeners,
  };
}